<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :rules="rules"
      :model="userInfo"
      label-width="120px"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width:300px" />
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="userInfo.password" type="password" style="width:300px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">更新</el-button>
        <el-button type="primary" @click="cancel">取消</el-button>
      </el-form-item>
      <!-- {{ $route.query.id }} id-->
    </el-form>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        id: this.$route.query.id,
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadUserDetailById()
  },
  methods: {
    async loadUserDetailById() {
      const res = await getUserDetailById(this.userInfo.id)
      console.log('获取原来列表', res)
      this.userInfo = res.data
    },
    // 保存更新的员工基本信息
    async doSave() {
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log('login-setting-保存修改结果', res)
        this.$message.success('操作成功')
        this.$router.back()
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    submit() {
      this.$refs.userForm.validate(valid => {
        valid && this.doSave()
      })
    },
    // 取消
    cancel() {
      this.$router.back()
    }
  }
}
</script>

<style>

</style>
